package com.victor.bingwallpaper.util;

import com.victor.bingwallpaper.bean.Images;

import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

public class FileUtils {
    private static Path readmePath = Paths.get("README.md");
    private static Path bingPath = Paths.get("bing-wallpaper.md");

    public FileUtils() {
    }

    public static List<Images> readBing() throws IOException {
        if (!Files.exists(bingPath, new LinkOption[0])) {
            Files.createFile(bingPath);
        }

        List<String> allLines = Files.readAllLines(bingPath);
        allLines = (List)allLines.stream().filter((sx) -> {
            return !sx.isEmpty();
        }).collect(Collectors.toList());
        List<Images> imgList = new ArrayList();
        imgList.add(new Images());

        for(int i = 1; i < allLines.size(); ++i) {
            String s = ((String)allLines.get(i)).trim();
            int descEnd = s.indexOf("]");
            int urlStart = s.lastIndexOf("(") + 1;
            String date = s.substring(0, 10);
            String desc = s.substring(14, descEnd);
            String url = s.substring(urlStart, s.length() - 1);
            imgList.add(new Images(desc, date, url));
        }

        return imgList;
    }

    public static void writeBing(List<Images> imgList) throws IOException {
        if (!Files.exists(bingPath, new LinkOption[0])) {
            Files.createFile(bingPath);
        }

        Files.write(bingPath, "## Bing Wallpaper".getBytes(), new OpenOption[0]);
        Files.write(bingPath, System.lineSeparator().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
        Iterator var1 = imgList.iterator();

        while(var1.hasNext()) {
            Images images = (Images)var1.next();
            Files.write(bingPath, images.formatMarkdown().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
            Files.write(bingPath, System.lineSeparator().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
            Files.write(bingPath, System.lineSeparator().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
        }

    }

    public static List<Images> readReadme() throws IOException {
        if (!Files.exists(readmePath, new LinkOption[0])) {
            Files.createFile(readmePath);
        }

        List<String> allLines = Files.readAllLines(readmePath);
        List<Images> imgList = new ArrayList();

        for(int i = 3; i < allLines.size(); ++i) {
            String content = (String)allLines.get(i);
            Arrays.stream(content.split("\\|")).filter((s) -> {
                return !s.isEmpty();
            }).map((s) -> {
                int dateStartIndex = s.indexOf("[", 3) + 1;
                int urlStartIndex = s.indexOf("(", 4) + 1;
                String date = s.substring(dateStartIndex, dateStartIndex + 10);
                String url = s.substring(urlStartIndex, s.length() - 1);
                return new Images((String)null, date, url);
            }).forEach(imgList::add);
        }

        return imgList;
    }

    public static Map<String, String> readReadmeMap() throws IOException {
        if (!Files.exists(readmePath, new LinkOption[0])) {
            Files.createFile(readmePath);
        }

        List<String> allLines = Files.readAllLines(readmePath);
        Map<String, String> imageMap = new HashMap();

        for(int i = 3; i < allLines.size(); ++i) {
            String content = (String)allLines.get(i);
            Arrays.stream(content.split("\\|")).filter((s) -> {
                return !s.isEmpty();
            }).forEach((s) -> {
                int dateStartIndex = s.indexOf("[", 3) + 1;
                int urlStartIndex = s.indexOf("(", 4) + 1;
                String date = s.substring(dateStartIndex, dateStartIndex + 10);
                String url = s.substring(urlStartIndex, s.length() - 1);
                imageMap.put(date, url);
            });
        }

        return imageMap;
    }

    public static void writeReadme(List<Images> imgList) throws IOException {
        if (!Files.exists(readmePath, new LinkOption[0])) {
            Files.createFile(readmePath);
        }

        Files.write(readmePath, "## Bing Wallpaper".getBytes(), new OpenOption[0]);
        Iterator var1 = imgList.iterator();

        while(var1.hasNext()) {
            Images images = (Images)var1.next();
            Files.write(readmePath, System.lineSeparator().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
            Files.write(readmePath, images.toLarge().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
            Files.write(readmePath, System.lineSeparator().getBytes(), new OpenOption[]{StandardOpenOption.APPEND});
        }

    }
}
